<template>
    <el-form :model="customerFollowUpForm" label-width="80px" >
    <el-row >
      <el-col :span="8">
        <el-form-item label="客户" prop="user">
           <el-input v-model="customerFollowUpForm.user" placeholder="请输入"/>
        </el-form-item>
      </el-col>
      <el-col :span="8">
        <el-form-item label="客户标签" prop="userLabel">
           <el-select v-model="customerFollowUpForm.userLabel" placeholder="请选择" style="width: 100%;">
             <el-option label="仓储" value="仓储" />
             <el-option label="青岛" value="青岛" />
           </el-select>
         </el-form-item>
      </el-col>
      <el-col :span="8">
        <el-form-item label="所属销售"  prop="affiliatedSales" >
           <el-select v-model="customerFollowUpForm.affiliatedSales" placeholder="请选择" style="width: 100%;">
             <el-option label="墨" value="墨" />
             <el-option label="简单" value="简单" />
             <el-option label="销售" value="销售" />
           </el-select>
         </el-form-item>
      </el-col>
    </el-row>
    <el-row >
      <el-col :span="8">
        <el-form-item label="跟进方式" prop="followUpMethods">
           <el-select v-model="customerFollowUpForm.followUpMethods" placeholder="请选择" style="width: 100%;">
             <el-option label="电话" value="电话" />
             <el-option label="短信" value="短信" />
             <el-option label="实物邮件" value="实物邮件" />
             <el-option label="当面" value="当面" />
           </el-select>
         </el-form-item>
      </el-col>
      <el-col :span="8">
        <el-form-item label="跟进日期" prop="followUpDate">
      <el-date-picker
        v-model="customerFollowUpForm.followUpDate"
        type="daterange"
        start-placeholder="开始日期"
        end-placeholder="结束日期"
      />
        </el-form-item>
      </el-col>
      <el-col :span="8">
        <el-form-item>
           <el-button type="primary">搜索</el-button>
           <el-button>重置</el-button>
        </el-form-item>
      </el-col>
    </el-row>
  </el-form>
  <!-- 按钮 -->
  <el-row class="mb-4 btnBox">
    <el-button type="primary" style="margin-left: 60px;" @click="addClientFollow = !addClientFollow">
      <el-icon><Plus/></el-icon>
      <span>新建</span>
    </el-button>
  </el-row>
  <!-- 表格 -->
  <el-table :data="customerFollowTable" style="width: 100%" :cell-style="{textAlign:'center'}" :header-cell-style="{'text-align': 'center',background:'#fafafa',color:'#606266' }">
    <el-table-column type="index" label="序号" width="100" />
    <el-table-column prop="name" label="客户" />
    <el-table-column prop="addTime" label="跟进时间" />
    <el-table-column prop="remark" label="记录内容" />
    <el-table-column prop="connectWay" label="跟进方式">
      <template #default="{row}">
        <span v-if="row.connectWay == 1">电话</span>
        <span v-if="row.connectWay == 2">短信</span>
        <span v-if="row.connectWay == 3">实物邮件</span>
        <span v-if="row.connectWay == 4">当面</span>
      </template>
    </el-table-column>
    <el-table-column prop="saleUserName" label="所属销售"/>
    <el-table-column label="操作" width="200px">
      <template #default="{row}">
        <el-button type="primary" link @click="getReviseUserInfo(row.id)">修改</el-button>
        <el-button type="primary" link >删除</el-button>
      </template>
    </el-table-column>
  </el-table>
  <!-- 分页 -->
  <el-pagination
      v-model:current-page="page"
      v-model:page-size="pageSize"
      layout="prev, pager, next, jump, sizes"
      :total="total"
      background 
      class="mt-4"
    />
  <!-- 新增---抽屉 -->
  <el-drawer
    v-model="addClientFollow"
    title="新增"
    direction="rtl"
    size="50%"
  >
  <addNewFollow :userList="userList" ref="FollowAddChild"/>
  <template #footer>
    <el-button @click="addClientFollow = false">取消</el-button>
    <el-button type="primary" @click="saveFollowList">确认</el-button>
  </template>
  </el-drawer>

  <!-- 修改--- 抽屉 -->
  <el-drawer
    v-model="clientFollowRevise"
    title="修改"
    direction="rtl"
    size="50%"
  >
  <reviseClientFollow :echoReviseFromData="echoReviseFromData"/>
  <template #footer>
    <el-button @click="clientFollowRevise = false">取消</el-button>
    <el-button type="primary">确认</el-button>
  </template>
  </el-drawer>
</template>

<script setup lang="ts">
import { Plus } from "@element-plus/icons-vue";
import {getFollowList,getUserList,getUserFollowDetail} from "@/api/followUp"
import {ref,onMounted} from "vue"
import addNewFollow from "./components/addNewFollow.vue"
import reviseClientFollow from "./components/reviseClientFollow.vue"

defineOptions({
  name: "CustomerFollowUp",
});
const FollowAddChild = ref<any>(null)
const saveFollowList = ()=>{
  FollowAddChild.value.sendFollowAdd()
}
const clientFollowRevise = ref(false)

const total = ref(0);
const page = ref(1);
const pageSize = ref(10); 
const customerFollowTable = ref([])
const addClientFollow = ref(false)
const customerFollowUpForm = ref({
  user: "",
  userLabel: "",
  affiliatedSales: "",
  followUpMethods: "",
  followUpDate: [],
})

const getReviseUserInfo=(val:any)=>{
  clientFollowRevise.value = !clientFollowRevise.value
  getFollowUserInfo(val)
}
let userList:Array<any> = []
const getUserFollowList = ()=>{
  getFollowList().then((res:any)=>{
    customerFollowTable.value = res.result.records
  })
}
// 获取用户的信息
const getUser = ()=>{
  getUserList().then((res:any)=>{
    if(res.code === 10000){
      userList = res.result
    }
  })
}
// 点击修改按钮发请求
// 定义变量去接收返回的数据
let echoReviseFromData = ref({})
const getFollowUserInfo = (userId:any)=>{
  getUserFollowDetail(userId).then((res:any)=>{
    if(res.code === 10000){
      echoReviseFromData.value = res.result
    }
  })
}
onMounted(()=>{
  getUserFollowList()
  getUser()
})
</script>

<style scoped>
.btnBox {
  margin-top: 10px;
  margin-bottom: 20px;
}
.el-pagination{
  display: flex;
  margin-top: 14px;
  margin-bottom: 14px;
  justify-content: flex-end;
  margin-right: 10px;
}
</style>